package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.ProductMap;
import com.etone.smartAudit.dto.productMap.ProductMapDTO;
import com.etone.smartAudit.enums.OnlineState;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.ProductMapMapper;
import com.etone.smartAudit.mapstruct.ProductMapMapstruct;
import com.etone.smartAudit.utils.BooleanUtil;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.productMap.ProductMapVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: sovy
 * @date: 2020/12/22
 * @description:
 */

@Service
public class ProductMapService extends ServiceImpl<ProductMapMapper, ProductMap> {

    @Autowired
    private ProductMapMapstruct productMapMapstruct;

    @Autowired
    private ProductMapTypeService productMapTypeService;

    /**
     * 查找列表
     *
     * @param key              关键字（能力代码、能力名称、适应范围）
     * @param abilityTypeIds   能力类别
     * @param serviceNatureIds 产品服务性质
     * @param startUpdateTime  编辑开始时间
     * @param endUpdateTime    编辑结束时间
     * @param onlineState      上线状态
     * @param page             页码
     * @param pageSize         分页大小
     * @return
     */
    public PagerVO<ProductMapVO> list(String key, List<Integer> abilityTypeIds, List<Integer> serviceNatureIds, Date startUpdateTime, Date endUpdateTime, OnlineState onlineState, int page, int pageSize) {
        Page<ProductMapVO> productMapVOPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> baseMapper.findList(key, abilityTypeIds, serviceNatureIds, startUpdateTime, endUpdateTime, onlineState));
        return PagerVO.of(productMapVOPage);
    }

    public ProductMap get(Integer id) {
        ProductMap productMap = getById(id);
        if (productMap == null) {
            throw new CustomParameterizedException("产品地图不存在");
        }
        return productMap;
    }

    /**
     * 判断是否存在
     *
     * @param id
     * @return
     */
    public Boolean exists(Integer id) {
        return baseMapper.exists(id);
    }

    /**
     * 判断是否存在,并抛异常
     *
     * @param id
     * @return
     */
    public void existsThrow(Integer id) {
        Boolean exists = exists(id);
        if (BooleanUtil.isFalse(exists)) {
            throw new CustomParameterizedException("产品地图不存在");
        }
    }

    /**
     * 添加|编辑产品地图
     *
     * @param id
     * @param productMapDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(Integer id, ProductMapDTO productMapDTO) {

        //判断类型是否存在
        productMapTypeService.existsThrow(productMapDTO.getTypeId());

        Date now = new Date();
        ProductMap productMap;
        if (id == null) {
            productMap = ProductMap.builder().createTime(now).build();
        } else {
            existsThrow(id);
            productMap = ProductMap.builder().id(id).build();
        }
        productMapMapstruct.copy2ProductMap(productMap, productMapDTO);
        productMap.setUpdateTime(now);
        saveOrUpdate(productMap);
    }

    /**
     * 产品地图详情
     *
     * @param id
     * @return
     */
    public ProductMapVO detail(Integer id) {
        return baseMapper.findProductMapVO(id);
    }

    /**
     * 删除产品地图
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        existsThrow(id);
        removeById(id);
    }
}
